package com.jane.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.jane.gzzb.CheckRecordsActivity;
import com.jane.gzzb.FavActivity;
import com.jane.gzzb.HostTimeActivity;
import com.jane.gzzb.LoginActivity;
import com.jane.gzzb.PlatformActivity;
import com.jane.gzzb.R;
import com.jane.view.RoundImageView;
import com.squareup.picasso.Picasso;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment implements View.OnClickListener {
    private LinearLayout mLL_attention,mLL_host,mLL_Ranking,mLL_History,mLL_Offline,mLL_Platform,mLL_Fav;
    private Button mButton;
    private RoundImageView ivTouxiang;
    private View view;

    public MineFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine, container, false);
        initView();
        initLisiten();
        return view;
    }

    private void initView() {
        mButton= (Button) view.findViewById(R.id.btn_login);
        ivTouxiang= (RoundImageView) view.findViewById(R.id.login_iv);
        mLL_attention= (LinearLayout) view.findViewById(R.id.tv_attention);
        mLL_Ranking= (LinearLayout) view.findViewById(R.id.tv_host_ranking);
        mLL_Platform= (LinearLayout) view.findViewById(R.id.tv_platform);
        mLL_Offline= (LinearLayout) view.findViewById(R.id.tv_offline);
        mLL_host= (LinearLayout) view.findViewById(R.id.tv_host);
        mLL_History= (LinearLayout) view.findViewById(R.id.tv_history);
        mLL_Fav= (LinearLayout) view.findViewById(R.id.tv_fav);
    }

    private void initLisiten() {
        mButton.setOnClickListener(this);
        mLL_attention.setOnClickListener(this);
        mLL_Ranking.setOnClickListener(this);
        mLL_Platform.setOnClickListener(this);
        mLL_Offline.setOnClickListener(this);
        mLL_host.setOnClickListener(this);
        mLL_History.setOnClickListener(this);
        mLL_Fav.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_iv://点击头像登录
                startActivity(new Intent(getActivity(),LoginActivity.class));
                break;
            case R.id.btn_login://马上登录
                startActivity(new Intent(getActivity(),LoginActivity.class));
                break;
            case R.id.tv_attention://我的关注
                //TODO
                break;
            case R.id.tv_host://主播时刻
                startActivity(new Intent(getActivity(),HostTimeActivity.class));
                break;
            case R.id.tv_host_ranking://主播排行
                //TODO 与我的关注跳到同一个界面
                break;
            case R.id.tv_history://观看记录
                //TODO
                startActivity(new Intent(getActivity(),CheckRecordsActivity.class));
                break;
            case R.id.tv_offline://离线视频
                //TODO
                break;
            case R.id.tv_platform://平台大全
                startActivity(new Intent(getActivity(),PlatformActivity.class));
                break;
            case R.id.tv_fav://我的收藏
                startActivity(new Intent(getActivity(),FavActivity.class));
                break;
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        ShareSDK.initSDK(getActivity());
        Platform platform = ShareSDK.getPlatform(getActivity(), QQ.NAME);
        if(platform.isAuthValid()){
            String userIcon = platform.getDb().getUserIcon();

            Picasso.with(getActivity()).load(userIcon).into(ivTouxiang);
            if (platform.getDb().getUserName()!=null){
                String userName=platform.getDb().getUserName();
                mButton.setText(userName);
            }
        }
        Platform platform2 = ShareSDK.getPlatform(getActivity(), Wechat.NAME);
        if(platform2.isAuthValid()){
            String userIcon = platform2.getDb().getUserIcon();
            Picasso.with(getActivity()).load(userIcon).into(ivTouxiang);
            if (platform.getDb().getUserName()!=null){
                String userName=platform.getDb().getUserName();
                mButton.setText(userName);
            }
        }
        Platform platform3 = ShareSDK.getPlatform(getActivity(), SinaWeibo.NAME);
        if(platform3.isAuthValid()){
            String userIcon = platform3.getDb().getUserIcon();
            Picasso.with(getActivity()).load(userIcon).into(ivTouxiang);
            if (platform.getDb().getUserName()!=null){
                String userName=platform.getDb().getUserName();
                mButton.setText(userName);
            }
        }
    }
}
